import * as types from '../constants/ActionTypes';

export default function login(state = [], action = {}) {
    const {
        type, payload
    } = action;
    
    switch (type) {
        //============================================================
        //用户登录
        //============================================================
        case types.LOGIN:
            return { ...payload };
        case types.LOGIN_SUCCESS:
            let successInfo =  {
                login_status: 1,
                message: "登录成功",
                khid : payload.results[0].khid
            };
            return {
                ...state,
                ...successInfo
            };
        case types.LOGIN_FAILURE:
            let failureInfo = {
                login_status: -1,
                message: payload.message||'网络连接失败'
            };
            return {
               ...state,
               ...failureInfo
            }; 

        //============================================================
        //退出登录
        //============================================================
        case types.LOGOUT:
            return { ...payload };
        case types.LOGOUT_SUCCESS:
            let logoutSuccessInfo =  {
                logout_status: 1,
                message: "退出登录成功"
            };
            return {
                ...state,
                ...logoutSuccessInfo
            };
        case types.LOGOUT_FAILURE:
            let logoutFailureInfo = {
                logout_status: -1,
                message: payload.message||'网络连接失败'
            };
            return {
               ...state,
               ...logoutFailureInfo
            };   

        //============================================================
        //资金账户绑定
        //============================================================

        case types.BIND:
            return { ...payload };
        case types.BIND_SUCCESS:

            let bindSuccessInfo = null;
            if (payload.results && payload.results.length) {
                bindSuccessInfo = {
                    bind_status : 1,
                    message : "绑定资金账户成功"
                };
            }else{
                bindSuccessInfo =  {
                    bind_status: -1,
                    message: "绑定资金账户失败"
                };
            }
            return {
                ...state,
                ...bindSuccessInfo
            };
        case types.BIND_FAILURE:
            let failureMsg;
            if (payload.message) {
                if(payload.message.indexOf("980023096")>=0){
                    failureMsg = "账号或密码错误";
                }else if(payload.message.indexOf("980023093")>=0){
                    failureMsg = "账号不允许该方式登录";
                }else if(payload.message.indexOf("980023093")>=0){
                    failureMsg = "账号已锁定";
                }else{
                    failureMsg = payload.message;
                }
            }else{
                failureMsg = "绑定失败，请重试";
            }

            let bindFailureInfo = {
                bind_status: -1,
                message: failureMsg
            };
            return {
               ...state,
               ...bindFailureInfo
            };  
        default:
            return state;
    }
}